//
//  YJSearchTextField.m
//  CToolBox
//
//  Created by crj on 16/8/30.
//  Copyright © 2016年 oyf. All rights reserved.
//

#import "YJSearchTextField.h"

static CGFloat const sureBtnWidth = 65;
static CGFloat const searchHeight = 30;

@interface YJSearchTextField()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *searchContentView;
@property (nonatomic, strong) UILabel *iconLabel;
@property (nonatomic, strong) UITextField *textField;

@property (nonatomic, strong) UIButton *sureBtn;

@end

@implementation YJSearchTextField


- (instancetype)initWithFrame:(CGRect)frame placeholder:(NSString *)placeholder isCanEdit:(BOOL)isCanEdit delegate:(id)delegate
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setConfig];
        _delegate = delegate;
        _placeholder = placeholder;
        _isCanEdit = isCanEdit;
        [self makeUI];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setConfig];
        [self makeUI];
    }
    return self;
}

- (void)setConfig{
    
   self.viewBackgroundColor = [UIColor clearColor];
    _placeholder = @"";
    _isCanEdit = NO;
}

- (void)addSureBtnToSuperView{
    _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureBtn.backgroundColor = HEXCOLOR(kColorBlue1, 1);
    _sureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_sureBtn setTitle:@"确认" forState:UIControlStateNormal];
    _sureBtn.layer.masksToBounds = YES;
    _sureBtn.layer.cornerRadius = kCornerRadius;
    [_sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_sureBtn];
}

- (void)makeUI{
    
    
    if(_isCanEdit){
        [self addSureBtnToSuperView];
    }
    
    _searchContentView = [[UIView alloc] init];
    _searchContentView.backgroundColor = [UIColor whiteColor];
    _searchContentView.layer.masksToBounds = YES;
    _searchContentView.layer.borderWidth = 0.5;
    _searchContentView.layer.borderColor = HEXCOLOR(kColorGray7, 1).CGColor;
    _searchContentView.layer.cornerRadius = kCornerRadius;
    [self addSubview:_searchContentView];
    
    _iconLabel = [[UILabel alloc] init];
    _iconLabel.font = [UIFont fontWithName:kFontIcon size:15];//30
    _iconLabel.text = theIconFont.iconweibiaoti106;

    _iconLabel.textColor = HEXCOLOR(kColorGray3, 1);
    _iconLabel.textAlignment = NSTextAlignmentCenter;
    [_searchContentView addSubview:_iconLabel];
    
    _textField = [[UITextField alloc] init];
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.delegate = self;
    [_textField setValue:HEXCOLOR(kColorGray2,1) forKeyPath:@"_placeholderLabel.textColor"];
    _textField.placeholder = _placeholder;
    _textField.text = _text;
    _textField.font = [UIFont systemFontOfSize:15];
    _textField.returnKeyType = UIReturnKeyDone;
    [_searchContentView addSubview:_textField];
    
    
    [_textField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
}

#pragma mark - Event Respond

- (void)sureAction{
    
    [_textField resignFirstResponder];
    if([_delegate respondsToSelector:@selector(searchTextFieldEditComplete:)]){
        [_delegate searchTextFieldEditComplete:_textField.text];
    }
}

- (void)textFieldEditingChanged:(UITextField *)textField{
    
    _text = textField.text;
    if([_delegate respondsToSelector:@selector(searchtextFieldEditingChanged:)]){
        [_delegate searchtextFieldEditingChanged:textField.text];
    }
    
}

- (void)textFieldBecomeFirstResponder{
    
    [_textField becomeFirstResponder];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [_textField resignFirstResponder];

    if([_delegate respondsToSelector:@selector(searchTextFieldEditComplete:)]){
        [_delegate searchTextFieldEditComplete:textField.text];
    }
    return NO;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if([_delegate respondsToSelector:@selector(searchTextFieldEditStart)]){
        [_delegate searchTextFieldEditStart];
    }
    return _isCanEdit;
}



- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    
    [self adjustSearchContent];
    
    
    _iconLabel.frame = CGRectMake(0, 0, _searchContentView.yj_height, _searchContentView.yj_height);
    _textField.frame = CGRectMake(_iconLabel.yj_right, 0, _searchContentView.yj_width-_iconLabel.yj_right, _searchContentView.yj_height);
}

- (void)adjustSearchContent{
    
    if(_sureBtn){//有确认按钮
        _sureBtn.frame = CGRectMake(self.yj_width-kSpaceSide-sureBtnWidth, kSpaceSide, sureBtnWidth, searchHeight);
        _searchContentView.frame = CGRectMake(kSpaceSide, kSpaceSide, _sureBtn.yj_x-kSpaceSide*2, self.yj_height-kSpaceSide*2);
    }else{//没有确认按钮
        _searchContentView.frame = CGRectMake(kSpaceSide, kSpaceSide, self.yj_width-kSpaceSide*2, self.yj_height-kSpaceSide*2);
    }
}

#pragma mark - SET

- (void)setPlaceholder:(NSString *)placeholder{
    
    _placeholder = placeholder;
    
    _textField.placeholder = _placeholder;
}

- (void)setText:(NSString *)text{
    
    _text = text;
    
    _textField.text = _text;
}



- (void)setViewBackgroundColor:(UIColor *)viewBackgroundColor{
    
    _viewBackgroundColor = viewBackgroundColor;
    
    self.backgroundColor = _viewBackgroundColor;
}

- (void)setIsShowSureBtn:(BOOL)isShowSureBtn{
    
    _isShowSureBtn = isShowSureBtn;
    if(!_sureBtn){
        return;
    }
    
    if(_isShowSureBtn){
        
        [self addSureBtnToSuperView];


    }else{
        [_sureBtn removeFromSuperview];
        _sureBtn = nil;

    }
    [self adjustSearchContent];
    
}


@end
